package com.tnsw.coreutils.text;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

import android.text.format.DateFormat;
import android.util.Log;

public class TextUtils {
	
	public static Object[] getValueFrom(String line, int initialIndex, String fromKey, String untilStt) {
		Object[] returnObjs = new Object[2];
		returnObjs[0] = "";
		returnObjs[1] = 0;
		
		int lastEndIndex;
		int startIdx = line.indexOf(fromKey, initialIndex);
		if (startIdx == -1) return(returnObjs);
		
		int endIdx = line.indexOf(untilStt, startIdx+1);
		if (endIdx == -1) {
			endIdx = line.length();
		}
		
		lastEndIndex = endIdx;
		
		String outputStt = line.substring(startIdx, endIdx);
		
		returnObjs[0] = outputStt;
		returnObjs[1] = lastEndIndex;
		
		return(returnObjs);
	}	
	
	public static String getValue(String line, String key, char quoteChar) {
		String tag = "TextUtils.getValue";
		
		int startIdx = line.indexOf(key);
		if (startIdx == -1) return("");
		
		int startPos = 0, endPos = 0;
		boolean found = false;
		
		int i;
		
		for (i = startIdx; i < line.length(); i++) {
			if (line.charAt(i) == quoteChar) {
				found = true;
				startPos = i;
				break;
			}
		}
		
		if (found == false) return("");
		found = false;
		
		for (i = startPos+1; i < line.length(); i++) {
			if (line.charAt(i) == quoteChar) {
				found = true;
				endPos = i;
				break;
			}
		}
		
		Log.d(tag, "startPos=" + String.valueOf(startPos) + ", endPos=" + String.valueOf(endPos));
		
		String stt = line.substring(startPos+1, endPos);
		return(stt);
	}	
	
	public static String getInsideQuote(String line, char quoteChar, int startIdx) {
		String tag = "TextUtils.getInsideQuote";
		int startPos = 0, endPos = 0;
		int i;
		boolean found = false;
		String insideStt = "";
		
		for (i = 0; i < line.length(); i++) {
			if (line.charAt(i) == quoteChar) {
				found = true;
				startPos = i;
				break;
			}
		}
		
		if (found == false) return("");
		
		for (i = line.length()-1; i >= startIdx; i--) {
			Log.d("TextUtils.getInsideQuote", ">> " + line.substring(i));
			if (line.charAt(i) == quoteChar) {
				found = true;
				endPos = i;
				Log.d(tag, "break");
				break;
				
			}
		}
		
		Log.d(tag, "(getInsideQuote) startPos=" + startPos);
		Log.d(tag, "(getInsideQuote) endPos=" + endPos);
		
		insideStt = line.substring(startPos+1, endPos);
						
		return(insideStt);
	}
	
	public static int indexOfBackward(String str, int startFrom, String s) {
		int ii = -1;
		
		int i = startFrom;
		for (i = startFrom; i >= 0; i--) {
			String ss = str.substring(i, i+s.length());
			if (ss.equals(s)) {
				return(i);
			}
		}
		
		return(ii);
	}
	
	public static String getJsonSimpleLevel(String mainJsonStr, String fieldName) {
		String tag = "TextUtils.getJsonSimpleLevel()";
		
		int idx = mainJsonStr.indexOf(fieldName);
		if (idx == -1) {
			Log.e(tag, "Cannot find fieldname=" + fieldName);
			return("");
		}
		
		int leftIdx = TextUtils.indexOfBackward(mainJsonStr, idx, "{");
		if (leftIdx == -1) { 
			Log.e(tag, "Cannot find left boundary {");
			return("");
		}
		
		int rightIdx = mainJsonStr.indexOf("}", idx);
		if (rightIdx == -1) {
			Log.e(tag, "Cannot find right boundary }");
			return("");
		}
		
		String stt = mainJsonStr.substring(leftIdx, rightIdx+1);
		return(stt);
	}

	public static String getDuration2(int durationSec) {
		// return 'xh:ym:zs'
		double hh = Math.floor(durationSec/(60*60));
		hh = Math.floor(hh);
		
		double hhSec = (int) hh * (60*60);
		double leftSec = durationSec-hhSec;
		
		double mm = Math.floor(leftSec/60);
		
		double ss = leftSec-((int) mm*60);
		
		String result = "";
		result = String.valueOf((int) hh) + ":" + String.valueOf((int) mm) + ":" + String.valueOf((int) ss);
		
		return(result);
	}
	
	public static String getDuration1(int durationSec) {
		// return 'xh:ym:zs'
		double hh = Math.floor(durationSec/(60*60));
		hh = Math.floor(hh);
		
		double hhSec = (int) hh * (60*60);
		double leftSec = durationSec-hhSec;
		
		double mm = Math.floor(leftSec/60);
		
		double ss = leftSec-((int) mm*60);
		
		String result = "";
		
		if ((int) hh != 0) {
			result = String.valueOf((int) hh) + "h:" + String.valueOf((int) mm) + "m:" + String.valueOf((int) ss) + "s";	
		} else {
			if ((int) mm != 0) {
				result = String.valueOf((int) mm) + "m:" + String.valueOf((int) ss) + "s";
			} else {
				result = String.valueOf((int) ss) + "s";
			}
		}
		
		
		return(result);
	}
	
	public static String getFinishWhen(int durationSec) {
		long c = System.currentTimeMillis();
		Date now = new Date(c);
		
		return getFinishWhen(now, durationSec);
	}
	
	public static String getFinishWhen(Date now, int durationSec) {
		
		// Support only tomorrow
		String result = "";
				
		Calendar cal = Calendar.getInstance();
		cal.setTime(now);
		int currentDate = cal.get(Calendar.DATE);
		
		cal.add(Calendar.SECOND, durationSec);
		int newDate = cal.get(Calendar.DATE);
		
		if (newDate > currentDate) {
			result = "tmr ";
		}
		
		result = result + (String) DateFormat.format("hh:mm:ss", cal.getTime());
		
		return(result);
	}
	
	public static void writeTextFile(String fileName, String str) {
		try {
			FileWriter fw = new FileWriter(fileName, true);
			BufferedWriter out = new BufferedWriter(fw);
			
			out.write(str);
			out.newLine();
			
			out.close();
			
		} catch (Exception e) {
			Log.e("TextUtils.writeTextFile", e.toString(), e);
		}
	}
	
	public static String getMyStandardDateStr(long mil) {
		Calendar c = Calendar.getInstance();
		c.setTimeInMillis(mil);
		String str = DateFormat.format("yyyy-MM-dd kk:mm:ss", c).toString();
		return(str);
	}
	
	public static String getMyStandardDateStr(long mil, String timezoneId) {
		Calendar c = Calendar.getInstance();
		c.setTimeInMillis(mil);
		c.setTimeZone(TimeZone.getTimeZone(timezoneId));
		String str = DateFormat.format("yyyy-MM-dd kk:mm:ss", c).toString();
		return(str);
	}

}
